package cn.lingyangwl.agile.infra.dict.controller;

import cn.lingyangwl.agile.infra.dict.service.*;
import cn.lingyangwl.agile.infra.module.dict.model.entity.*;
import cn.lingyangwl.agile.infra.module.dict.model.rqrs.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 * 
 * @author shenguangyang
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/dict")
public class DictController {
    @Resource
    private DictService dictService;
    @Resource
    private DictItemService dictItemService;

    @ApiOperation("获取字典列表")
    @GetMapping("/page")
    @RamResource(action = "sys:dict:query", name = "查询字典", type = ActionType.READ)
    public Resp<IPage<DictResp>> page(DictQueryReq query) {
        IPage<DictResp> page = dictService.pageDict(query);
        return Resp.ok(page);
    }

    @ApiOperation("查询全部字典条目")
    @GetMapping("/listAllDictItem")
    public Resp<Map<String, List<DictItem>>> listAllDictItem() {
        Map<String, List<DictItem>> result = dictService.listAllDictItem();
        return Resp.ok(result);
    }

    @RamResource(action = "sys:dict:export", name = "导出字典", type = ActionType.READ)
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, DictQueryReq req) throws Exception {
        dictService.exportExcel(response, req);
    }


    @RamResource(action = "sys:dict:import", name = "导入字典", type = ActionType.WRITE)
    @PostMapping("/importExcel")
    public Resp<String> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        String resp = dictService.importExcel(inputStream);
        return Resp.ok(resp);
    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation("查询字典")
    @RamResource(action = "sys:dict:query", name = "查询字典", type = ActionType.READ)
    @GetMapping(value = "/get")
    public Resp<Dict> get(@RequestParam(value = "id") Long dictId) {
        return Resp.ok(dictService.getById(dictId));
    }

    /**
     * 加载字典
     */
    @ApiOperation("加载字典")
    @RamResource(action = "sys:dict:query", name = "加载字典", type = ActionType.READ)
    @GetMapping("/loadDict")
    public Resp<List<DictResp>> loadDict(@RequestParam(value = "keyword", required = false) String keyword ) {
        if (StringUtils.isEmpty(keyword)) {
            return Resp.ok(Collections.emptyList());
        }
        List<DictResp> resp = dictService.loadDict(keyword);
        return Resp.ok(resp);
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典")
    @PostMapping("/save")
    @RamResource(action = "sys:dict:save", name = "保存字典", type = ActionType.WRITE)
    public Resp<?> save(@Validated @RequestBody Dict dict) {
        return dictService.save(dict) ? Resp.ok() : Resp.fail("保存失败");
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PostMapping("/update")
    @RamResource(action = "sys:dict:update", name = "更新字典", type = ActionType.WRITE)
    public Resp<?> update(@Validated @RequestBody Dict dict) {
        return dictService.updateById(dict) ? Resp.ok() : Resp.fail("更新失败");
    }

    /**
     * 删除字典类型
     */
    @RamResource(action = "sys:dict:delete", name = "删除字典", type = ActionType.WRITE)
    @ApiOperation("批量删除字典类型")
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] dictIds) {
        dictService.deleteBatchByIds(dictIds);
        return Resp.ok();
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @RamResource(action = "sys:dict:delete", name = "删除字典", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") @NotEmpty(message = "字典id是必填项") String dictId) {
        dictService.deleteById(Long.valueOf(dictId));
        return Resp.ok();
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation("刷新字典缓存")
    @RamResource(action = "sys:dict:refreshCache", name = "刷新字典缓存", type = ActionType.WRITE)
    @GetMapping("/refreshCache")
    public Resp<?> refreshCache() {
        dictService.resetDictCache();
        dictItemService.resetDictCache();
        return Resp.ok();
    }
}